<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Flash extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();


	}
	function create_xml($year,$month,$tourId,$userId,$site)
	{
		$query="SELECT id FROM  `users` WHERE  `site` LIKE  '%$site%'";
		$res=$this->db->query($query);
		if($res->num_rows()>0)
		{
			$userId=$res->row()->id;
		}
		else
		{
			$this->log($site." not in users");
			exit();
		}
		$result=$this->db->query('Select id,Name from service where id="'.$tourId.'"');
		if ($result->num_rows() > 0)
		{
   			$row = $result->row();
			$result_to_xml['title']  =  $row->Name;
			$query="select `desc` from listoptions where idOption=5 and idService=".$tourId;
			$res=$this->db->query($query);
			if($res->num_rows()>0)
			{
				$row2 = $res->row();
				$result_to_xml['days']   =  $row2->desc;
			}
			else 
			{
				$result_to_xml['days']   =  0;
			}
			$query="select `desc` from listoptions where idOption=6 and idService=".$tourId;
			$res=$this->db->query($query);
			if($res->num_rows()>0)
			{
				$row2 = $res->row();
				$result_to_xml['nights']   =  $row2->desc;
			}
			else
			{
				$result_to_xml['nights']   =  0;
			}
			$result_to_xml['year'] =  $year;
			$result_to_xml['month'] =  $month;
			$query="select `desc` from listoptions where idOption=1 and idService=".$tourId;
			$res=$this->db->query($query);
			if($res->num_rows()>0)
			{
				$row2 = $res->row();
				$result_to_xml['home']   =  $row2->desc;
			}
			else
			{
				$result_to_xml['home']   =  0;
			}
			$query="select `desc` from listoptions where idOption=9 and idService=".$tourId;
			$res=$this->db->query($query);
			if($res->num_rows()>0)
			{
				$row2 = $res->row();
				$result_to_xml['podarok']   =  $row2->desc;
			}
			else
			{
				$result_to_xml['podarok']   =  "";
			}
			$result_to_xml['photo'] =  "";
			$query="select `desc` from listoptions where idOption=2 and idService=".$tourId;
			$res=$this->db->query($query);
			$i=1;
			foreach($res->result() as $r)
			{
				$query="select name,`desc` from service where id='".$r->desc."'";
				$res=$this->db->query($query);
				if($res->num_rows()>0)
				{
				$result_to_xml['e'.$i]['name'] =  $res->row()->name;
				$result_to_xml['e'.$i]['desc'] =  $res->row()->desc;
				$i++;
				}
				
			}	
		}
		else
		{
			$this->Log('Dont have this tourId in table '.$year.' - '.$month.' - '.$tourId);
			die();
		}
		$result_to_xml['arraydays']=array();
		if($month==12)
		{
			$nMonth=1;
			$nYear=$year+1;
		}
		else
		{
			$nMonth=$month+1;
			$nYear=$year;
		}
		$types=array("DBL","SNGL","TRPL","CHLD","HB");
		$query="select id,name from listoftables where idService=".$tourId;
		$result=$this->db->query($query);
		foreach($result->result() as $row)
		{
			for($i=1;$i<=5;$i++)
			{
				$query="Select price,`date` from price where `date`>='".$year."-".$month."-01' and `date`<'".$nYear."-".$nMonth."-01' and idTable=".$row->id." and idRow=".$i;
				//echo $query;
				$res=$this->db->query($query);
				foreach($res->result() as $rr)
				{
					$j=(int)substr($rr->date,8);
					$query="select price,type from agentprice where `date`='".$rr->date."' and idrow=".$i." and idService=".$tourId." and userId=".$userId;
					$r2=$this->db->query($query);
					if($r2->num_rows()>0)
					{
						$p=$r2->row()->price;
						$t=$r2->row()->type;
						if($r2->row()->type==1)
						{
							$rr->price+=(int)$rr->price*$r2->row()->price/100;
						}
						else
						{
							$rr->price+=$r2->row()->price;
						}
					}
					$result_to_xml['arraydays'][$j][substr($row->name,0,1)][$types[$i-1]]	= $rr->price;
				}
			}
		}
		//print_r($result_to_xml);
		return $result_to_xml;
	}
	function create_knowing($year,$month,$id,$userId,$site)
	{
		$query="SELECT id FROM  `users` WHERE  `site` LIKE  '%$site%'";
		$res=$this->db->query($query);
		if($res->num_rows()>0)
		{
			$userId=$res->row()->id;
		}
		else
		{
			$this->log($site." not in users");
			//exit();
		}
		$result_to_xml['year'] =  $year;
		$result_to_xml['month'] =  $month;
		$result_to_xml['arraydays']=array();
		if($month==12)
		{
			$nMonth=1;
			$nYear=$year+1;
		}
		else
		{
			$nMonth=$month+1;
			$nYear=$year;
		}
		$types=array("DBL","SNGL","TRPL","CHLD","HB");
		$query="select idService,name from listoftables where id=".$id;
		$result=$this->db->query($query);
		if($result->num_rows>0)
		{
			$tourId=$result->row()->idService;
			
			$result_to_xml['name']=htmlspecialchars($result->row()->name);
			$result_to_xml['options']='';
			$result_to_xml['descr']="";
			$query="select id,`desc` from service where name like '%".$result->row()->name."%'";
			$re2=$this->db->query($query);
			if($re2->num_rows()>0)
			{
				$descHotel=$re2->row()->desc;
				
				if($descHotel!=""){$result_to_xml['descr']=$descHotel;}
				
				$idHotel=$re2->row()->id;
				$query="select `desc` from listoptions where (idOption=16 or idOption=18) and idService=".$idHotel;
				$dop=$this->db->query($query);
				$i=0;
				foreach($dop->result() as $row)
				{
					$result_to_xml['options'][$i]['id']=$row->desc;
					$query="select name from options where id=".$row->desc;
					$dop2=$this->db->query($query);
					foreach($dop2->result() as $row2)
					{
						$result_to_xml['options'][$i]['name']=$row2->name;
					}
					$i++;
				}
			}
		}
		else
		{
			die('mysql error');
		}
		#foreach($result->result() as $row)
		#{
		
			for($i=1;$i<=5;$i++)
			{
			$query="Select price,`date` from price where `date`>='".$year."-".$month."-01' and `date`<'".$nYear."-".$nMonth."-01' and idTable=".$id." and idRow=".$i;
			//echo $query;
			$res=$this->db->query($query);
			foreach($res->result() as $rr)
			{
			$j=(int)substr($rr->date,8);
			$query="select price,type from agentprice where `date`='".$rr->date."' and idrow=".$i." and idService=".$tourId." and userId=".$userId;
			$r2=$this->db->query($query);
			if($r2->num_rows()>0)
			{
				$p=$r2->row()->price;
				$t=$r2->row()->type;
				if($r2->row()->type==1)
				{
					$rr->price+=(int)$rr->price*$r2->row()->price/100;
				}
				else
				{
					$rr->price+=$r2->row()->price;
				}
			}
				$result_to_xml['arraydays'][$j][$types[$i-1]]	= $rr->price;
			}
		#}
		}
		//print_r($result_to_xml);
		return $result_to_xml;
	}
	function month($year,$month){

		if($month==12)
		{
			$nMonth=1;
			$nYear=$year+1;
		}
		else
		{
			$nMonth=$month+1;
			$nYear=$year;
		}

		$result_to_xml="";
		$query='Select COUNT(*) as count from price where `date`>="'.$nYear.'-'.$nMonth.'-01"';
		$result=$this->db->query($query);
		if ($result->num_rows() > 0)
		{
   			if($result->row()->count>0)
   			{
				$result_to_xml['NextMonth'] =  'Yes';
			}
			else
			{
				$result_to_xml['NextMonth'] =  'No';
			}
   		}
		
   		return $result_to_xml;
	}
	function id($idCountry,$type)
	{
		$query="select id from service where idUser=".$idCountry." and idtype=".$type;
		$result=$this->db->query($query);
		foreach($result->result() as $row)
		{
			$result_to_xml['service'][]=$row->id;
		}
		return $result_to_xml;
	}
}